package com.fengye.middleware.rabbitmq.boot.messageType.direct;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author sky
 * @date 2021年06月09日 1:21 下午
 */
@Component
public class DirectExchangeConfig {

    @Value("${mq.direct.queue.one.name:direct_queue_one}")
    private String directQueueNameOne;

    @Value("${mq.direct.queue.two.name:direct_queue_two}")
    private String directQueueNameTwo;

    @Value("${mq.direct.exchange.name:direct_change_name}")
    private String directExchangeName;

    @Value("${mq.direct.routing.key.one.name:direct_routing_key_one_name}")
    private String directRoutingKeyOneName;


    @Value("${mq.direct.routing.key.two.name:direct_routing_key_two_name}")
    private String directRoutingKeyTwoName;


    /* 创建简单消息模型：队列、交换机和路由 */

    // 队列
    @Bean(name = "directQueueOne")
    public Queue directQueueOne() {
        return new Queue(directQueueNameOne);
    }

    @Bean(name = "directQueueTwo")
    public Queue directQueueTwo() {
        return new Queue(directQueueNameTwo);
    }

    @Bean(name = "directExchange")
    public DirectExchange directExchange() {
        return new DirectExchange(directExchangeName, true, false);
    }


    /**
     * 将队列 1 绑定到交换机上
     *
     * @param directQueueOne 队列 1
     * @param directExchange 交换机
     * @return org.springframework.amqp.core.Binding 绑定
     */
    @Bean
    public Binding directBindingOne(Queue directQueueOne, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueueOne).to(directExchange).with(directRoutingKeyOneName);
    }

    /**
     * 将队列 2 绑定到交换机上
     *
     * @param directQueueTwo 队列 2
     * @param directExchange 交换机
     * @return org.springframework.amqp.core.Binding 绑定结果
     */
    @Bean
    public Binding directBindingTwo(Queue directQueueTwo, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueueTwo).to(directExchange).with(directRoutingKeyTwoName);
    }
}
